package com.baoji.auth.controller;

import com.baoji.auth.model.AccountErrorCode;
import com.baoji.auth.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @Auther: ghm
 * @Date: 2021/09/23/17:48
 * @Description:
 */

@Api(tags = "登录控制器")
@RestController
@RequestMapping("/auth")
public class AuthController {

    @Autowired
    private UserService userService;

    @ApiOperation(value = "用户注册", notes = "不需要登录")
    @PostMapping("/register")
    public String registerUser(@RequestBody Map<String,String> registerUser) {

        String username = registerUser.get("username");
        String password = registerUser.get("password");

        if (StringUtils.isBlank(username)) {
            // 用户名为空
            return AccountErrorCode.USERNAME_ISNONULL.getMessage();
        }else if (StringUtils.isBlank(password)){
            // 密码为空
            return AccountErrorCode.PASSWORD_ISNONULL.getMessage();
        }

        if (!userService.cheakUser(username)) {
            // 该用户已注册，不能重复注册
            return AccountErrorCode.USERNAME_ERROR.getMessage();
        }

        userService.save(username, password);

        return "success";
    }
}
